package tcm.test.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tcm.test.mapper.UserMapper;
import tcm.test.pojo.dto.UserDTO;
import tcm.test.pojo.entity.Order;
import tcm.test.pojo.entity.User;
import tcm.test.pojo.vo.OrderVO;
import tcm.test.service.UserService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //注册用户
    public void register(UserDTO userDTO) {
        User u = new User();
        BeanUtils.copyProperties(userDTO, u);
        u.setCreateTime(LocalDateTime.now());
        u.setUpdateTime(LocalDateTime.now());
        userMapper.register(u);
    }

    //根据id查询用户
    public User getById(Long id) {
        return userMapper.getById(id);

    }

    //根据id更新用户
    public void update(UserDTO userDTO) {
        User u = new User();
        BeanUtils.copyProperties(userDTO, u);
        u.setUpdateTime(LocalDateTime.now());
        userMapper.update(u);
    }

    //根据id删除用户
    public void delete(Long id) {
        userMapper.delete(id);
    }

    //获取指定用户的所有订单列表
    public List<OrderVO> getOrders(Long id) {
        List<Order> orderList =userMapper.getOrders(id);
        List<OrderVO> orderVOList = new ArrayList<>();

        for (Order order : orderList) {
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(order, orderVO);
            orderVOList.add(orderVO);
        }

        return orderVOList;
    }
}
